// pages/your-page/your-page.js
Page({
  data: {
    folders: [
      { name: '图片', iconUrl: '../../images/wjj.png' },
      { name: '视频', iconUrl: '../../images/wjj.png' },
      { name: '文档', iconUrl: '../../images/wjj.png' },
      { name: '其他', iconUrl: '../../images/wjj.png' }
    ],
    check:true,
    start:0,
    isShow:0,
    messages:'',
    file:[]
  },
  show(e){
    let avatarurl = wx.getStorageSync('avatarurl')
    let username = wx.getStorageSync('username')
    if(!avatarurl||!username){
      wx.showToast({
        title: '请前往我的页面登录后进行后续操作',
        icon: 'none'
      });
      return;
    }
    wx.navigateTo({
      url: '../listShow/listShow?Type='+this.data.folders[e.currentTarget.dataset.id].name
    });
  },
  search(){
    let avatarurl = wx.getStorageSync('avatarurl')
    let username = wx.getStorageSync('username')
    if(!avatarurl||!username){
      wx.showToast({
        title: '请前往我的页面登录后进行后续操作',
        icon: 'none'
      });
      return;
    }
    wx.navigateTo({
      url: '../search/search',
    })
  },
  onFileChange() {
    let avatarurl = wx.getStorageSync('avatarurl')
    let username = wx.getStorageSync('username')
    if(!avatarurl||!username){
      wx.showToast({
        title: '请前往我的页面登录后进行后续操作',
        icon: 'none'
      });
      return;
    }
    const self = this;
    if(!self.data.check){
      wx.showToast({
        title: '已有在上传文件，请耐心等待',
        icon: 'none'
      });
      return;
    }
    self.data.check=false;
    wx.chooseMessageFile({
      count: 1,
      // 最多可以选择的文件数量，默认为1
      success(res) {
        console.log(res)
        self.data.file=res.tempFiles
        self.setData({
          isShow:1,
          start:0,
          messages:"上传中..."
        })
        const size = self.data.file[0].size
        wx.uploadFile({
          url: "https://zdw666.fun/wjy/upload",
          // 开发者服务器URL
          filePath: self.data.file[0].path,
          // 要上传文件资源的路径
          name: "multipartFile",
          // 必填，后台用来解析的文件名
          formData: {
            "bucket": "nullbucket",
            "fileName":self.data.file[0].name,
            "openId":wx.getStorageSync("openid")
          },success(uploadRes) {
            self.setData({
              isShow:1,
              start:1,
              messages:'上传成功'
            })
            setTimeout(function(){
              self.setData({
                isShow:0
              })
            },2000)
            self.data.check=true;
          },fail(uploadErr) {
            self.setData({
              isShow:2,
              start:2,
              messages:'上传失败,点击重试'
            })
            self.data.check=true;
            setTimeout(function(){
              self.setData({
                isShow:0
              })
            },2000)
          }
        });
      },fail(err) {
        wx.showToast({
          title: '取消选择文件',
          icon: 'none'
        });
        self.data.check=true;
      }
    });
  },
  uploads(){
    const self = this;
    wx.uploadFile({
      url: "https://zdw666.fun/wjy/upload",
      // 开发者服务器URL
      filePath: self.data.file[0].path,
      // 要上传文件资源的路径
      name: "multipartFile",
      // 必填，后台用来解析的文件名
      formData: {
        "bucket": "nullbucket",
        "fileName":self.data.file[0].name,
        "openId":wx.getStorageSync("openid")
      },success(uploadRes) {
        self.setData({
          isShow:1,
          start:1,
          messages:'上传成功'
        })
        self.data.check=true;
        setTimeout(function(){
          self.setData({
            isShow:0
          })
        },2000)
      },fail(uploadErr) {
        self.setData({
          isShow:2,
          start:2,
          messages:'上传失败,点击重试'
        })
        self.data.check=true;
        setTimeout(function(){
          self.setData({
            isShow:0
          })
        },2000)
      }
    })
  }
});